/**
 * 
 */
package edu.tsinghua.mrpvc.device.lightsensor;

import java.util.Calendar;
import java.util.Hashtable;
import java.util.Map;

import org.osgi.framework.BundleContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.service.event.EventHandler;
import org.osgi.util.tracker.ServiceTracker;

import edu.tsinghua.mrpvc.device.LightSensor;
import edu.tsinghua.mrpvc.device.simulate.LightSensorSimulateTopic;



/**
 * @author Tangjian Deng & Wei Feng
 *
 */
public class LightSensorImpl implements LightSensor, EventHandler{
	String ID;
	int lightLevel;
	boolean valid;
	private ServiceTracker tracker;
	
	public LightSensorImpl(BundleContext context, String ID){
		this.ID = ID;
		tracker = new ServiceTracker(context, EventAdmin.class.getName(), null);
		tracker.open();
	}
	@Override
	public String getID() {
		// TODO Auto-generated method stub
		return ID;
	}

	@Override
	public int getLightLevel() {
		// TODO Auto-generated method stub
		return lightLevel;
	}

	@Override
	public boolean isValid() {
		// TODO Auto-generated method stub
		return valid;
	}


	public void sense(){
		System.out.print(Calendar.getInstance().getTime().toLocaleString() + " ");
		//System.out.println("LightSensor: sense executed");
		setLightLevel(15);
	}
	
	private void generEvent(){
		EventAdmin eventAdmin = (EventAdmin) tracker.getService();
		
		if(eventAdmin != null){	
			Map<String, String> propers = new Hashtable<String, String>();
			propers.put(LightSensor.EVENT_PROPER_ID, ID);
			propers.put(LightSensor.EVENT_PROPER_ISVALID, valid == true ? LightSensor.Constants.ISVALID_TRUE : LightSensor.Constants.ISVALID_FALSE);
			propers.put(LightSensor.EVENT_PROPER_LIGHT_LEVEL, (new Integer(lightLevel)).toString());
			Event event = new Event(LightSensor.EVENT_TOPIC_STATE, propers);
			eventAdmin.postEvent(event);
		}
		
	}
	private int setLightLevel(int level) {
		// TODO Auto-generated method stub
		lightLevel = level;
		System.out.println(Calendar.getInstance().getTime().toLocaleString() + 
				" LightSensor: sense light level and post event");
		generEvent();
		return lightLevel;
	}
	@Override
	public void handleEvent(Event event) {
		// TODO Auto-generated method stub
		
		if(event.getTopic().equals(LightSensor.EVENT_TOPIC_ISVALID)){
			if(event.getProperty(LightSensor.EVENT_PROPER_ID) != null && event.getProperty(LightSensor.EVENT_PROPER_ID).equals(ID)){
				generEvent();
			}
			else{
				System.out.println("***Lightsensor: ID not match***");
			}
		}
		else if(event.getTopic().equals(LightSensor.EVENT_TOPIC_LIGHTLEVEL)){
			if(event.getProperty(LightSensor.EVENT_PROPER_ID) != null && event.getProperty(LightSensor.EVENT_PROPER_ID).equals(ID)){
				generEvent();
			}
			else{
				System.out.println("***Lightsensor: ID not match***");
			}
		}		
		else if(event.getTopic().equals(LightSensorSimulateTopic.LIGHT_SENSE)){
			sense();	
		}
	}
}
